/**
 * @description 登录、获取用户信息、退出登录、清除token逻辑，不建议修改
 */
import { useAclStore } from './acl'
// import { useTabsStore } from './tabs'
import { useRoutesStore } from './routes'
// import { useSettingsStore } from './settings'
import { UserModuleType } from '/#/store'
import {
  getUserInfo,
  login,
  socialLogin,
  socialQrLogin,
  logout,
} from '@/api/user'
import { getToken, removeToken, setToken } from '@/utils/token'
import { getJsSign } from '@/utils/wxjssdk'
import { resetRouter } from '@/router'
import { isArray, isString } from '@/utils/validate'
import { tokenName } from '@/config'
import { gp } from '@gp'

export const useUserStore = defineStore('user', {
  state: (): UserModuleType => ({
    token: getToken() as string,
    corpId: '',
    userId: '',
    name: '游客',
    alias: '游客',
    mainDepartmentId: 0,
    avatar: 'https://i.gtimg.cn/club/item/face/img/2/15922_100.gif',
  }),
  getters: {
    getToken: (state) => state.token,
    getCorpId: (state) => state.corpId,
    getUserId: (state) => state.userId,
    getName: (state) => state.name,
    getAlias: (state) => state.alias,
    getMainDepartmentId: (state) => state.mainDepartmentId,
    getAvatar: (state) => state.avatar,
  },
  actions: {
    /**
     * @description 设置token
     * @param {*} token
     */
    setToken(token: string) {
      this.token = token
      setToken(token)
    },
    /**
     * @description 设置企业id
     * @param {*} corpId
     */
    setCorpId(corpId: string) {
      this.corpId = corpId
    },
    /**
     * @description 设置企业微信用户id
     * @param {*} userId
     */
    setUserId(userId: string) {
      this.userId = userId
    },
    /**
     * @description 设置用户名
     * @param {*} name
     */
    setName(name: string) {
      this.name = name
    },
    /**
     * @description 设置用户昵称
     * @param {*} alias
     */
    setAlias(alias: string) {
      this.alias = alias
    },
    /**
     * @description 设置主属部门
     * @param {*} username
     */
    setMainDepartmentId(mainDepartmentId: number) {
      this.mainDepartmentId = mainDepartmentId
    },
    /**
     * @description 设置头像
     * @param {*} avatar
     */
    setAvatar(avatar: string) {
      this.avatar = avatar
    },
    /**
     * @description 登录拦截放行时，设置虚拟角色
     */
    setVirtualRoles() {
      const aclStore = useAclStore()
      aclStore.setFull(true)
      this.setCorpId('')
      this.setUserId('')
      this.setName('admin(未开启登录拦截)')
      this.setAlias('admin(未开启登录拦截)')
      this.setMainDepartmentId(0)
      this.setAvatar('https://i.gtimg.cn/club/item/face/img/2/15922_100.gif')
    },
    /**
     * @description 设置token并发送提醒
     * @param {string} token 更新令牌
     * @param {string} tokenName 令牌名称
     */
    afterLogin(token: string, tokenName: string) {
      // const settingsStore = useSettingsStore()
      if (token) {
        this.setToken(token)
        // const hour = new Date().getHours()
        // const thisTime =
        //   hour < 8
        //     ? '早上好'
        //     : hour <= 11
        //     ? '上午好'
        //     : hour <= 13
        //     ? '中午好'
        //     : hour < 18
        //     ? '下午好'
        //     : '晚上好'
        // gp.$baseNotify(`欢迎登录`, `${thisTime}！`)
      } else {
        const err = `登录接口异常，未正确返回${tokenName}...`
        gp.$baseMessage(err, 'error', 'vab-hey-message-error')
        throw err
      }
    },
    /**
     * @description 登录
     * @param {*} userInfo
     */
    async login(userInfo: any) {
      const {
        data: { [tokenName]: token },
      } = await login(userInfo)
      this.afterLogin(token, tokenName)
    },
    /**
     * @description 第三方登录
     * @param {*} tokenData
     */
    async socialLogin(tokenData: any) {
      const {
        data: { [tokenName]: token },
      } = await socialLogin(tokenData)
      this.afterLogin(token, tokenName)
    },
    /**
     * @description 第三方登录
     * @param {*} tokenData
     */
    async socialQrLogin(tokenData: any) {
      const {
        data: { [tokenName]: token },
      } = await socialQrLogin(tokenData)
      this.afterLogin(token, tokenName)
    },
    /**
     * @description 获取用户信息接口 这个接口非常非常重要，如果没有明确底层前逻辑禁止修改此方法，错误的修改可能造成整个框架无法正常使用
     * @returns
     */
    async getUserInfo() {
      const {
        data: {
          corpId,
          userId,
          name,
          alias,
          mainDepartmentId,
          avatar,
          permissions,
        },
      } = await getUserInfo()
      /**
       * 检验返回数据是否正常，无对应参数，将使用默认用户名,头像,Roles和Permissions
       * userId {String}
       * avatar {String}
       * roles {List}
       * ability {List}
       */
      if (
        (userId && !isString(userId)) ||
        (avatar && !isString(avatar)) ||
        (permissions && !isArray(permissions))
      ) {
        const err = 'getUserInfo核心接口异常，请检查返回JSON格式是否正确'
        gp.$baseMessage(err, 'error', 'vab-hey-message-error')
        throw err
      } else {
        const aclStore = useAclStore()
        // 如不使用username用户名,可删除以下代码
        if (corpId) this.setCorpId(corpId)
        if (userId) this.setUserId(userId)
        if (name) this.setName(name)
        if (alias) this.setAlias(alias)
        if (mainDepartmentId) this.setMainDepartmentId(mainDepartmentId)
        // 如不使用avatar头像,可删除以下代码
        if (avatar) this.setAvatar(avatar)
        // 如不使用permissions权限控制,可删除以下代码
        if (permissions) aclStore.setPermission(permissions)
        if (permissions && permissions.includes('admin')) aclStore.setFull(true)

        //微信登录用户初始化jssdk
        getJsSign()
      }
    },
    /**
     * @description 退出登录
     */
    async logout() {
      await logout()
      await this.resetAll()
      // 解决横向布局退出登录显示不全的bug
      location.reload()
    },
    /**
     * @description 重置token、roles、permission、router等
     */
    async resetAll() {
      this.setToken('')
      this.setCorpId('')
      this.setUserId('')
      this.setName('游客')
      this.setAlias('游客')
      this.setMainDepartmentId(0)
      this.setAvatar('https://i.gtimg.cn/club/item/face/img/2/15922_100.gif')

      const aclStore = useAclStore()
      const routesStore = useRoutesStore()
      aclStore.setPermission([])
      aclStore.setFull(false)
      routesStore.clearRoutes()
      await resetRouter()
      removeToken()
    },
  },
})
